package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.options.ScreenOrientation;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.define.iDefines;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Main_Activity extends GameEngine
{
	
	State_Logo				stateLogo;
	State_Loading			state_Loading;
	State_Main_Menu			stateMainMenu;
	public State_Game_Play	stateGamePlay;
	State_Select_Player		statePlayer;
	State_Game_Pause		stateGamePause;
	State_Game_Win			stateGameWin;
	State_Game_Over			stateGameOver;
	State_Select_Arena		stateArena;
	State_Select_Rival		stateRival;
	State_Credits			stateCredits;
	State_Select_Mode		stateMode;
	
	@Override
	public void GameInit()
	{
		SCREEN_WIDTH = iDefines.MASTER_WIDTH;
		SCREEN_HEIGHT = iDefines.MASTER_HEIGHT;
		
		ORIENTATION = ScreenOrientation.LANDSCAPE_FIXED;
		
		if (ORIENTATION == ScreenOrientation.PORTRAIT_FIXED || ORIENTATION == ScreenOrientation.PORTRAIT_SENSOR)
		{
			CAMERA_WIDTH = SCREEN_WIDTH;
			CAMERA_HEIGHT = SCREEN_HEIGHT;
		}
		else if (ORIENTATION == ScreenOrientation.LANDSCAPE_FIXED || ORIENTATION == ScreenOrientation.LANDSCAPE_SENSOR)
		{
			CAMERA_WIDTH = SCREEN_WIDTH;
			CAMERA_HEIGHT = SCREEN_HEIGHT;
		}
		
		// server_Client = new BT_Server_Client(this);
	}
	
	@Override
	public void GameUpdate()
	{
		GameSetUpdate();
		
		GameStateMachine();
		// Utils.TRACE("state massage "+messageCurrentState);
		// Utils.TRACE("state  "+gameCurrentState);
	}
	
	void GameSetUpdate()
	{
		gameCurrentState = gameNextState;
		messageCurrentState = messageNextState;
	}
	
	void GameStateMachine()
	{
		switch (gameCurrentState)
		{
			case iGameDefines.GAME_STATE_INIT:
			{
				GameState_Init();
			}
				break;
			case iGameDefines.GAME_STATE_LOGO:
			{
				GameState_Logo();
			}
				break;
			case iGameDefines.GAME_STATE_LOADING:
			{
				GameState_Loading();
			}
				break;
			
			case iGameDefines.GAME_STATE_MAINMENU:
			{
				GameState_Mainmenu();
			}
				break;
			
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
			{
				GameState_SelectPlayer();
			}
				break;
			
			case iGameDefines.GAME_STATE_SELECT_RIVAL:
			{
				GameState_SelectRival();
			}
				break;
			
			case iGameDefines.GAME_STATE_SELECT_MAP:
			{
				GameState_SelectMap();
			}
				break;
			case iGameDefines.GAME_STATE_GAMEPLAY:
			{
				GameState_Gameplay();
			}
				break;
			case iGameDefines.GAME_STATE_SETTINGS:
			{
				GameState_Settings();
			}
				break;
			case iGameDefines.GAME_STATE_MODE:
			{
				GameState_Mode();
			}
				break;
			case iGameDefines.GAME_STATE_HELP:
			{
				GameState_Help();
			}
				break;
			case iGameDefines.GAME_STATE_ABOUT:
			{
				GameState_About();
			}
				break;
			case iGameDefines.GAME_STATE_QUIT:
			{
				GameState_Quit();
			}
				break;
			
			case iGameDefines.GAME_STATE_PAUSE:
			{
				GameState_Pause();
			}
				break;
			
			case iGameDefines.GAME_STATE_WIN:
			{
				GameState_Win();
			}
				break;
			
			case iGameDefines.GAME_STATE_OVER:
			{
				GameState_Over();
			}
				break;
		}
	}
	
	void GameState_Init()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			
			SetNextMessageState(NextMessageState());
			
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			SetNextMessageState(NextMessageState());
			
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
	}
	
	void GameState_Logo()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (stateLogo == null)
			{
				stateLogo = new State_Logo(this);
				
				stateLogo.Init();
				
			}
			else
				stateLogo.Init();
			
			stateMainMenu = new State_Main_Menu(this);
			stateMode = new State_Select_Mode(this);
			
			stateCredits = new State_Credits(this);
			stateArena = new State_Select_Arena(this);
			
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateLogo.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateLogo.Exit();
		}
	}
	
	void GameState_Loading()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			state_Loading = new State_Loading(this);
			state_Loading.Init();
			
			statePlayer = new State_Select_Player(this);
			stateRival = new State_Select_Rival(this);
			
			if (stateGamePlay == null)
			{
				stateGamePlay = new State_Game_Play(this);
			}
			
			if (stateGameOver == null)
			{
				stateGameOver = new State_Game_Over(this);
			}
			if (stateGamePause == null)
			{
				stateGamePause = new State_Game_Pause(this);
			}
			if (stateGameWin == null)
			{
				stateGameWin = new State_Game_Win(this);
			}
			
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			state_Loading.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			state_Loading.Exit();
		}
	}
	
	void GameState_Mainmenu()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (stateMainMenu != null)
			{
				stateMainMenu.Init();
				
			}
			
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateMainMenu.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateMainMenu.exit();
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
			// SetNextGameState(iGameDefines.GAME_STATE_GAMEPLAY);
			
		}
	}
	
	private void GameState_SelectPlayer()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (statePlayer != null)
			{
				
				statePlayer.Init();
				
				SetNextMessageState(NextMessageState());
			}
			//
			//
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			statePlayer.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			statePlayer.exit();
			
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
	}
	
	private void GameState_SelectRival()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (stateRival != null)
			{
				stateRival.Init();
				SetNextMessageState(NextMessageState());
			}
		}
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateRival.Update();
		}
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateRival.exit();
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	private void GameState_SelectMap()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (statePlayer != null)
			{
				stateArena.Init();
				SetNextMessageState(NextMessageState());
			}
		}
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateArena.Update();
		}
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateArena.Exit();
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	void GameState_Gameplay()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			stateGamePlay.Init();
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateGamePlay.Update();
			// SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(iGameDefines.GAME_STATE_PAUSE);
		}
	}
	
	private void GameState_Pause()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			
			stateGamePause.Init();
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateGamePause.Update();
			// SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	private void GameState_Win()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			
			Utils.TRACE("sssssssssss");
			stateGameWin.Init();
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateGameWin.Update();
			// SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	private void GameState_Over()
	{
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			
			stateGameOver.Init();
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateGameOver.Update();
			// SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			SetNextMessageState(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	private void GameState_Quit()
	{
		
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			// if(stateQuit == null)
			// {
			// stateQuit.Init();
			// }
			
			SetNextMessageState(NextMessageState());
		}
		// else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		// {
		//
		// // SetNextMessageState(NextMessageState());
		// }
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			// SetNextQuit(NextMessageState());
			SetNextGameState(NextGameState());
		}
		
	}
	
	private void GameState_About()
	{
		
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (stateCredits != null) stateCredits.Init();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateCredits.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateCredits.Exit();
			
		}
		
	}
	
	private void GameState_Help()
	{
		// TODO Auto-generated method stub
		
	}
	
	private void GameState_Mode()
	{
		
		if (messageCurrentState == iGameDefines.MESSAGE_STATE_INIT)
		{
			if (stateMode != null) stateMode.Init();
			
			SetNextMessageState(NextMessageState());
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_UPDATE)
		{
			stateMode.Update();
		}
		else if (messageCurrentState == iGameDefines.MESSAGE_STATE_EXIT)
		{
			stateMode.Exit();
			SetNextMessageState(NextMessageState());
			
			if (GameEngineConfiguration.ModeSelected == iGameDefines.MODE_CAREER)
			{
				SetNextGameState(iGameDefines.GAME_STATE_GAMEPLAY);
			}
			else
				SetNextGameState(NextGameState());
			
		}
		
	}
	
	private void GameState_Settings()
	{
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		switch (gameCurrentState)
		{
			case iGameDefines.GAME_STATE_MAINMENU:
			{
				if (stateMainMenu == null) { return false; }
				
				stateMainMenu.keyBack(keyCode, event);
			}
				break;
			case iGameDefines.GAME_STATE_MODE:
				if (stateMode == null) { return false; }
				stateMode.keyDown(keyCode, event);
				break;
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
				if (statePlayer == null) { return false; }
				
				statePlayer.keyBack(keyCode, event);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_RIVAL:
				if (stateRival == null) { return false; }
				
				stateRival.keyBack(keyCode, event);
				break;
			
			case iGameDefines.GAME_STATE_GAMEPLAY:
				if (stateGamePlay == null) { return false; }
				
				stateGamePlay.keyBack(keyCode, event);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_MAP:
				if (stateArena == null) { return false; }
				stateArena.keyDown(keyCode, event);
				break;
			
			case iGameDefines.GAME_STATE_ABOUT:
				if (stateCredits == null) { return false; }
				stateCredits.keyDown(keyCode, event);
				break;
		
		}
		return false;
	}
	
	public boolean onSceneTouchEvent(Scene arg0, TouchEvent arg1)
	{
		
		mClickDetector.onTouchEvent(arg1);
		mScrollDetector.onTouchEvent(arg1);
		switch (gameCurrentState)
		{
			case iGameDefines.GAME_STATE_MAINMENU:
				
				if (stateMainMenu == null) { return false; }
				
				stateMainMenu.SceneTouch(arg0, arg1);
				
				break;
			
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
				if (statePlayer == null) { return false; }
				
				statePlayer.SceneTaouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_GAMEPLAY:
				
				Utils.TRACE("ddd");
				if (stateGamePlay == null) { return false; }
				Utils.TRACE("ccc");
				stateGamePlay.SceneTouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_PAUSE:
				
				if (stateGamePause == null) { return false; }
				// stateGamePause.SceneTouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_OVER:
				
				Utils.TRACE("ddd");
				if (stateGameOver == null) { return false; }
				// stateGameOver.SceneTouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_WIN:
				
				Utils.TRACE("ddd");
				if (stateGameWin == null) { return false; }
				// stateGameWin.SceneTouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_RIVAL:
				if (stateRival == null) { return false; }
				
				stateRival.SceneTaouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_MAP:
				if (stateArena == null) { return false; }
				
				stateArena.SceneTaouch(arg0, arg1);
				break;
			
			case iGameDefines.GAME_STATE_ABOUT:
				if (stateCredits == null) { return false; }
				
				stateCredits.SceneTaouch(arg0, arg1);
				break;
		
		}
		
		Utils.TRACE("eee");
		
		return false;
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		
		switch (gameCurrentState)
		{
			case iGameDefines.GAME_STATE_MAINMENU:
			{
				if (stateMainMenu == null) { return false; }
				stateMainMenu.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			}
				break;
			
			case iGameDefines.GAME_STATE_GAMEPLAY:
			{
				if (stateGamePlay == null) { return false; }
				stateGamePlay.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			}
				break;
			case iGameDefines.GAME_STATE_PAUSE:
			{
				if (stateGamePause == null) { return false; }
				stateGamePause.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			}
				break;
			case iGameDefines.GAME_STATE_OVER:
			{
				if (stateGameOver == null) { return false; }
				stateGameOver.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			}
				break;
			case iGameDefines.GAME_STATE_WIN:
			{
				if (stateGameWin == null) { return false; }
				stateGameWin.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
			}
				break;
			case iGameDefines.GAME_STATE_MODE:
				if (stateMode == null) { return false; }
				stateMode.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
				break;
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
				if (statePlayer == null) return false;
				
				statePlayer.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
				
				break;
			
			case iGameDefines.GAME_STATE_SELECT_RIVAL:
				if (stateRival == null) { return false; }
				Utils.TRACE("HAHAHAHAHAH");
				stateRival.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_MAP:
				if (stateArena == null) { return false; }
				Utils.TRACE("HAHAHAHAHAH");
				stateArena.Touch(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
				break;
		
		}
		
		return false;
	}
	
	public void onClick(ClickDetector pClickDetector, int pPointerID, float pSceneX, float pSceneY)
	{
		switch (gameCurrentState)
		{
		
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
				statePlayer.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_RIVAL:
				stateRival.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
				break;
			
			case iGameDefines.GAME_STATE_SELECT_MAP:
				stateArena.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
				break;
			case iGameDefines.GAME_STATE_MODE:
				stateMode.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
				break;
		
		}
		
	}
	
	public void onScroll(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		switch (gameCurrentState)
		{
		
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
			{
				if (statePlayer == null)
				{
					break;
				}
				
				statePlayer.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
				// Utils.TRACE("KEPENCET DAP");
			}
				break;
			
			case iGameDefines.GAME_STATE_ABOUT:
			{
				if (stateCredits == null)
				{
					break;
				}
				
				stateCredits.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
				// Utils.TRACE("KEPENCET DAP");
			}
				break;
		
		}
	}
	
	public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		switch (gameCurrentState)
		{
		
			case iGameDefines.GAME_STATE_SELECT_PLAYER:
			{
				if (statePlayer == null)
				{
					break;
				}
				
				statePlayer.onScrollFinish(pScollDetector, pPointerID, pDistanceX, pDistanceY);
				
			}
				break;
		
		}
		
	}
	
	public void onScrollStarted(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		// TODO Auto-generated method stub
		
	}
	
}
